﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class LoaiVanChuyenDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            LoaiVanChuyenDTO objectDTO = new LoaiVanChuyenDTO();
            objectDTO.MaLoaiVC = int.Parse(dt.Rows[i]["MaLoaiVC"].ToString());
            objectDTO.TenLoaiVC = dt.Rows[i]["TenLoaiNV"].ToString();
            objectDTO.Gia = float.Parse(dt.Rows[i]["Gia"].ToString());
            return (object)objectDTO;
        }

        public LoaiVanChuyenDTO[] GetAll()
        {
            String strSQL = "Select * from LOAIVANCHUYEN ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            LoaiVanChuyenDTO[] loaivanChuyenDTOs = new LoaiVanChuyenDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                loaivanChuyenDTOs[i] = (LoaiVanChuyenDTO)GetDataFromDataRow(dt, i);

            return loaivanChuyenDTOs;
        }

        public void insert(LoaiVanChuyenDTO info)
        {
            string insertCommand = "INSERT LOAIVANCHUYEN VALUES(" +
                info.MaLoaiVC + ", '" +
                info.TenLoaiVC + "', " +
                info.Gia + ")";

            ExecuteNonQuery(insertCommand);
        }

        public void update(LoaiVanChuyenDTO info)
        {
            string updateCommand = "UPDATE LOAIVANCHUYEN " +
                                    "SET TenLoaiVC = '" + info.TenLoaiVC + "', " +
                                    " Gia = '" + info.Gia +
                                    " WHERE MaLoaiVC = " + info.MaLoaiVC;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(string maLoaiVC)
        {
            string deleteCommand = "DELETE FROM LOAIVANCHUYEN WHERE MaLoaiVC = " + maLoaiVC;
            ExecuteNonQuery(deleteCommand);
        }
    }
}
